package com.relytosoft.mizar.media.analyze.services.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyVehicleInfo;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyVehicleInfoDto;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyVehicleInfoVo;
import com.relytosoft.mizar.media.analyze.services.service.system.AyVehicleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/13 16:33
 */
@RestController
@RequestMapping("analyze/system/vehicleinfo")
@Api(tags = "分析系统基础接口")
public class AyVehicleInfoController {

    @Resource
    private AyVehicleInfoService ayVehicleInfoService;


    @SaCheckPermission("system:vehicle:list")
    @OpLog(msg = "车辆信息查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("车辆信息查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(AyVehicleInfoDto ayVehicleInfoDto, PageQuery pageQuery) {
        AyVehicleInfo ayVehicleInfo = BeanCopyUtils.copy(ayVehicleInfoDto, AyVehicleInfo.class);
        IPage<AyVehicleInfo> page = ayVehicleInfoService.selectAyVehicleInfoPageList(pageQuery, ayVehicleInfo);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @SaCheckPermission("system:vehicle:add")
    @OpLog(msg = "新增车辆", opType = OpTypeEnum.INSERT)
    @ApiOperation("新增车辆")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody AyVehicleInfoDto ayVehicleInfoDto) {
        AyVehicleInfo ayVehicleInfo = BeanCopyUtils.copy(ayVehicleInfoDto, AyVehicleInfo.class);
        boolean b = ayVehicleInfoService.addAyVehicleInfo(ayVehicleInfo);
        return b ? ResponseResult.ok("新增车辆成功") : ResponseResult.fail("新增车辆失败");
    }

    @SaCheckPermission("system:vehicle:id")
    @OpLog(msg = "根据ID查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("根据ID查询")
    @GetMapping({"{vehicleId}"})
    public ResponseResult<AyVehicleInfoVo> id(@PathVariable Integer vehicleId) {
        return ResponseResult.ok(ayVehicleInfoService.selectByVehicleId(vehicleId));
    }


    @SaCheckPermission("system:vehicle:remove")
    @OpLog(msg = "删除", opType = OpTypeEnum.DELETE)
    @ApiOperation("删除")
    @DeleteMapping("del")
    public ResponseResult<Void> remove(@RequestBody List<Integer> list) {
        boolean b = ayVehicleInfoService.updateAyVehicleInfoStatus(list);
        return b ? ResponseResult.ok("删除成功") : ResponseResult.fail("删除失败");
    }

    @SaCheckPermission("system:vehicle:update")
    @OpLog(msg = "修改", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyVehicleInfoDto ayVehicleInfoDto) {
        AyVehicleInfo ayVehicleInfo = BeanCopyUtils.copy(ayVehicleInfoDto, AyVehicleInfo.class);
        boolean b = ayVehicleInfoService.updateAyVehicleInfo(ayVehicleInfo);
        return b ? ResponseResult.ok("修改成功") : ResponseResult.fail("修改失败");
    }

    @OpLog(msg = "模板", opType = OpTypeEnum.EXPORT)
    @ApiOperation("模板")
    @GetMapping("import/template")
    public void importTemplate(HttpServletResponse response) {
        ayVehicleInfoService.importTemplate(response);
    }

    @SaCheckPermission("system:vehicle:import")
    @OpLog(msg = "批量导入车辆信息", opType = OpTypeEnum.IMPORT)
    @ApiOperation("批量导入车辆信息")
    @PostMapping(value = "import/{ids}")
    public ResponseResult<Void> importVehicleInfo(MultipartFile file,
                                                  @PathVariable("ids") List<Integer> ids) {
        //上传过来的excel文件
        ayVehicleInfoService.saveVehicleInfo(file, ayVehicleInfoService, ids);
        return ResponseResult.ok();
    }
}
